package com.jahooty;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.feature.WorldGenMinable;
import net.minecraftforge.fml.common.IWorldGenerator;

import com.jahooty.blocks.JahootyBlocks;

/**
 * Class that is used to modify the world generation process to add
 * our own items for the mod in to the world.
 * 
 * @author Michael
 */
public class EventManager implements IWorldGenerator
{
	/**
	 * Method that is called when the world is being generated.  We
	 * use this time to inject our custom items in to the world as it
	 * is being generated.
	 * 
	 * @param random Random object that is used as the random generator
	 * 				 with the world seed
	 * @param chunkX The chunk x-coordinate that is being generated, as an int
	 * @param chunkZ The chunk z-coordinate that is being generated, as an int
	 * @param world World object that represents the world that is being generated
	 * @param chunkGenerator IChunkProvider object that represents the generator
	 * 						 that is running to generate the world
	 * @param chunkProvider IChunkProvider object that represents the provider
	 * 						that is requesting the chunk
	 */
	@Override
	public void generate( Random random, 
						  int chunkX, 
						  int chunkZ, 
						  World world,
						  IChunkProvider chunkGenerator, 
						  IChunkProvider chunkProvider )
	{
		switch ( world.provider.getDimensionId() )
		{
			// Nether Creation
			case -1:
				
				break;
				
			// Overworld Creation
			case 0:
				
				// Add Copper Ore
				
				this.addOreSpawn( JahootyBlocks.copper_ore, 
								  world, 
								  random, 
								  chunkX * 16,
								  chunkZ * 16,
								  16,
								  16,
								  6 + random.nextInt( 3 ), 
								  8, 
								  1,
								  63 );
				
				break;
				
			// End Creation
			case 1:
				
				break;
				
			default:
				
				break;
		}
	}

	/**
     * Adds an Ore Spawn to Minecraft. Simply register all Ores to spawn with this method in your Generation method in your IWorldGeneration extending Class
     *
     * @param block The Block to spawn
     * @param world The World to spawn in
     * @param random A Random object for retrieving random positions within the world to spawn the Block
     * @param blockXPos An int for passing the X-Coordinate for the Generation method
     * @param blockZPos An int for passing the Z-Coordinate for the Generation method
     * @param maxX An int for setting the maximum X-Coordinate values for spawning on the X-Axis on a Per-Chunk basis
     * @param maxZ An int for setting the maximum Z-Coordinate values for spawning on the Z-Axis on a Per-Chunk basis
     * @param maxVeinSize An int for setting the maximum size of a vein
     * @param chancesToSpawn An int for the Number of chances available for the Block to spawn per-chunk
     * @param minY An int for the minimum Y-Coordinate height at which this block may spawn
     * @param maxY An int for the maximum Y-Coordinate height at which this block may spawn
     **/
	private void addOreSpawn( Block block,
							  World world, 
							  Random random, 
							  int blockXPos, 
							  int blockZPos,
							  int maxX, 
							  int maxZ, 
							  int maxVeinSize, 
							  int chancesToSpawn, 
							  int minY, 
							  int maxY )
    {
          assert maxY > minY: "The maximum Y must be greater than the Minimum Y";
          
          assert maxX > 0 && maxX <= 16: "addOreSpawn: The Maximum X must be greater than 0 and less than 16";
          
          assert minY > 0: "addOreSpawn: The Minimum Y must be greater than 0";
          
          assert maxY < 256 && maxY > 0: "addOreSpawn: The Maximum Y must be less than 256 but greater than 0";
          
          assert maxZ > 0 && maxZ <= 16: "addOreSpawn: The Maximum Z must be greater than 0 and less than 16";
         
          int diffBtwnMinMaxY = maxY - minY;
          
          for(int x = 0; x < chancesToSpawn; x++)
          {
                 int posX = blockXPos + random.nextInt( maxX );
                 
                 int posY = minY + random.nextInt( diffBtwnMinMaxY );
                 
                 int posZ = blockZPos + random.nextInt( maxZ );
                 
                 ( new WorldGenMinable( block.getDefaultState(), maxVeinSize ) ).generate( world, random, new BlockPos( posX, posY, posZ ) );
          }
    }
}
